<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Users extends CI_Controller {
    /* PAGES */

    /**
     * Constructeur de la class Users
     */
    public function __construct() {
        parent::__construct();
        
        $this->acl->setModule('users');
    }
    
    /**
     * Page d'administration des utilisateurs.
     */
    public function admin() {
        $this->acl->can('manage');

        $crud = new grocery_CRUD();

        $crud->set_table('S_BUSINESS_DATA.V_USER_USR')
                ->set_subject('un utilisateur')
                ->set_primary_key('USR_ID')

                // Champs.
                ->columns('CVL_ID', 'USR_LAST_NAME', 'USR_FIRST_NAME', 'USR_LOGIN', 'USR_EMAIL')
                ->add_fields('CVL_ID', 'USR_LAST_NAME', 'USR_FIRST_NAME', 'USR_LOGIN', 'USR_EMAIL', 'USR_PASSWORD', 'PASS2', 'USR_EXTERNAL_ACCESS')
                ->edit_fields('CVL_ID', 'USR_LAST_NAME', 'USR_FIRST_NAME', 'USR_LOGIN', 'USR_EMAIL')
                ->required_fields('CVL_ID', 'USR_LAST_NAME', 'USR_FIRST_NAME', 'USR_LOGIN', 'USR_EMAIL', 'USR_PASSWORD', 'PASS2', 'USR_EXTERNAL_ACCESS')

                // Types de champs.
                ->change_field_type('USR_PASSWORD', 'password')
                ->change_field_type('PASS2', 'password')

                // Affichage.
                ->display_as('CVL_ID', 'Civilité')
                ->display_as('PASS2', 'Resaisir mot de passe')
                ->display_as('USR_EMAIL', 'E-mail')
                ->display_as('USR_EXTERNAL_ACCESS', 'Acces externe')
                ->display_as('USR_FIRST_NAME', 'Prénom')
                ->display_as('USR_LAST_NAME', 'NOM')
                ->display_as('USR_LOGIN', 'Identifiant')
                ->display_as('USR_PASSWORD', 'Mot de passe')

                // Rules.
                ->set_rules('USR_EMAIL', 'Email', 'trim|valid_email|required');
        ;

        if ('add' === $crud->getState() || 'edit' === $crud->getState())
            $crud->set_relation('CVL_ID', 'S_BUSINESS_DATA.T_P_CIVILITY_CVL', 'CVL_CIVILITY');


        $crud->order_by('USR_LAST_NAME', 'ASC');

        // Callbacks
        $crud->callback_delete(array($this, 'callbackUserDelete'));
        $crud->callback_insert(array($this, 'callbackUserInsert'));
        $crud->callback_update(array($this, 'callbackUserUpdate'));

        $this->db = $this->load->database('DB_PLASTPROD', true);
        $output = $crud->render();

        $css = $crud->get_css_files();
        $js = $crud->get_js_files();

        $this->layout
                ->setTitle('Utilisateurs')
                ->setCss($css)
                ->setJs($js)
                ->view('users/admin', $output)
                ->display()
        ;
    }

    /* CALLBACKS */

    /**
     * Callback de suppression d'un utilisateur.
     * 
     * @param   int     $id
     * @return  boolean
     */
    public function callbackUserDelete($id) {
        if ($this->acl->can('manage', false)) {
            $datas = array(
                $id
            );

            $result = callProc('DB_PLASTPROD', 'S_BUSINESS_DATA', 'P_D_USER_00', $datas);

            if ('SUCCESS' === $result->RESULT)
                return true;
        }
        return false;
    }

    /**
     * Callback de création d'un utilisateur.
     * 
     * @param   array   $post
     * @return  boolean
     */
    public function callbackUserInsert($post) {
        if ($this->acl->can('manage', false)) {
            $datas = array(
                $post['CVL_ID'],
                '\'' . pg_escape_string($post['USR_LOGIN']) . '\'',
                '\'' . pg_escape_string($post['USR_PASSWORD']) . '\'',
                '\'' . pg_escape_string($post['USR_FIRST_NAME']) . '\'',
                '\'' . pg_escape_string($post['USR_LAST_NAME']) . '\'',
                '\'' . pg_escape_string($post['USR_EMAIL']) . '\'',
                '\'' . $post['USR_EXTERNAL_ACCESS'] . '\'',
            );

            $result = callProc('DB_PLASTPROD', 'S_BUSINESS_DATA', 'P_I_USER_00', $datas);

            if ('SUCCESS' === $result->RESULT)
                return true;
        }
        return false;
    }

    /**
     * Callback de mise à jour d'un utilisateur.
     * 
     * @param   array   $post
     * @param   int     $id
     * @return  boolean
     */
    public function callbackUserUpdate($post, $id) {
        if ($this->acl->can('manage', false)) {
            $datas = array(
                $id,
                $post['CVL_ID'],
                '\'' . pg_escape_string($post['USR_LOGIN']) . '\'',
                '\'' . pg_escape_string($post['USR_FIRST_NAME']) . '\'',
                '\'' . pg_escape_string($post['USR_LAST_NAME']) . '\'',
                '\'' . pg_escape_string($post['USR_EMAIL']) . '\''
            );

            $result = callProc('DB_PLASTPROD', 'S_BUSINESS_DATA', 'P_U_USER_00', $datas);

            if ('SUCCESS' === $result->RESULT)
                return true;
        }
        return false;
    }

}